/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.openwaf.client.ui.panels;

import com.openwaf.client.core.Window;
import com.openwaf.client.dom.DivElement;
import com.openwaf.client.dom.Element;
import com.openwaf.client.dom.LIElement;
import com.openwaf.client.dom.Node;
import com.openwaf.client.dom.OListElement;
import com.openwaf.client.event.dom.ClickEvent;
import com.openwaf.client.event.dom.ClickHandler;
import com.openwaf.client.ui.UIControl;
import com.openwaf.client.ui.annotation.ViewElement;
import com.openwaf.client.ui.annotation.ViewMethod;
import com.openwaf.client.ui.annotation.ViewProperty;
import java.util.ArrayList;
import java.util.Iterator;

public class StackPanel extends UIControl implements Iterable<StackPanelElement> {

    private ArrayList<StackPanelElement> elements;
    @ViewElement

    private int selectedIndex;
    public static String CSS_TITLE_NORMAL = "waf-panel-stack-element-title";
    public static String CSS_TITLE_SELECTED = "waf-panel-stack-element-title-selected";
    public static String CSS_CONTENT = "waf-panel-stack-element-content";
    private String cssTitleNormal = CSS_TITLE_NORMAL;
    private String cssTitleSelected = CSS_TITLE_SELECTED;
    private String cssContent = CSS_CONTENT;

    @ViewProperty
    public void setCssTitleNormal(String name) {
        cssTitleNormal = name;
        updateCss();
    }

    public String getCssTitleNormal() {
        return cssTitleNormal;
    }

    @ViewProperty
    public void setCssTitleSelected(String name) {
        cssTitleSelected = name;
        updateCss();
    }

    public String getCssTitleSelected() {
        return cssTitleSelected;
    }

    @ViewProperty
    public void setCssContent(String name) {
        cssContent = name;
        updateCss();
    }

    public String getCssContent() {
        return cssContent;
    }

    public void updateCss() {
        int count = 0;
        for (StackPanelElement elm : elements) {
            if (count == selectedIndex) {
                elm.setCssClassName(cssTitleSelected);
            } else {
                elm.setCssClassName(cssTitleNormal);
            }
            elm.getContentDiv().setCssClassName(cssContent);
        }
    }

    public StackPanel() {
        selectedIndex = -1;
        elements = new ArrayList<StackPanelElement>();
    }

    @ViewMethod
    public void addElement(final StackPanelElement tab) {
        getElement().appendChild(tab.getElement());
        tab.getTitleDiv().addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                select(tab);
            }
        });
        tab.getContentDiv().getStyle().setDisplay("none");
        tab.getContentDiv().setCssClassName(cssContent);
        tab.getTitleDiv().setCssClassName(cssTitleNormal);
        elements.add(tab);
        if (elements.size() == 1) {
            select(0);
        }
    }

    public void addElement(int index, final StackPanelElement tab) {
        if (index < 0 || index >= elements.size()) {
            throw new IndexOutOfBoundsException("Index:" + index + ",Size:" + elements.size());
        }
        StackPanelElement ex = elements.get(index);
        ex.getContentDiv().setCssClassName(cssContent);
        getElement().insertBefore(tab.getElement(), ex.getElement());
        elements.add(index, ex);
    }

    private void select(int index) {
        if (selectedIndex >= 0) {
            StackPanelElement elm = elements.get(selectedIndex);
            elm.getTitleDiv().setCssClassName(cssTitleNormal);
            elm.getContentDiv().getStyle().setDisplay("none");
        }
        StackPanelElement elm = elements.get(index);
        elm.getTitleDiv().setCssClassName(cssTitleSelected);
        elm.getContentDiv().getStyle().setDisplay("");
        selectedIndex = index;
    }

    private void select(StackPanelElement tab) {
        int index = getIndex(tab);
        if (index < 0) {
            return;
        }
        select(index);
    }

    public int getIndex(StackPanelElement tab) {
        int count = 0;
        for (StackPanelElement t : elements) {
            if (t.equals(tab)) {
                return count;
            }
            count += 1;
        }
        return -1;
    }

    public Iterator<StackPanelElement> iterator() {
        return elements.iterator();
    }
}
